<template>
  <div class="equipment-manager">
    <div class="header">
      <h2>机器管理</h2>
      <el-button type="primary" @click="handleAdd">新增机器</el-button>
    </div>

    <el-table 
      :data="equipmentList" 
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="equipmentName" label="机器编号" />
      <el-table-column prop="remark" label="备注" />
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag 
            :type="scope.row.isDelete === 0 ? 'success' : 'danger'"
            effect="light"
          >
            {{ scope.row.isDelete === 0 ? '未删除' : '已删除' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button 
            type="primary" 
            link
            @click="handleEdit(row)"
          >
            编辑
          </el-button>
          <el-button 
            type="danger" 
            link
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[5, 10, 50, 100]"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      :title="isEdit ? '编辑机器' : '新增机器'"
      v-model="dialogVisible"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="机器编号" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入机器编号" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input 
            v-model="form.remark" 
            type="textarea" 
            rows="3"
            placeholder="请输入备注信息" 
          />
        </el-form-item>
        <!-- 添加状态选择单选按钮组 -->
        <el-form-item label="站点状态" prop="isDelete" v-if="isEdit">
          <el-radio-group v-model="form.isDelete">
            <el-radio :label="0">未删除</el-radio>
            <el-radio :label="1">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from '../plugins/axios'

// 数据列表
const equipmentList = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 表单相关
const dialogVisible = ref(false)
const isEdit = ref(false)
const formRef = ref(null)
const form = ref({
  id: null,
  equipmentName: '',
  remark: '',
  isDelete: 0
})

const rules = {
  equipmentName: [
    { required: true, message: '请输入机器编号', trigger: 'blur' }
  ],
  remark: [
    { required: false, message: '请输入备注信息', trigger: 'blur' }
  ]
}

// 获取机器列表
const fetchEquipmentList = async () => {
  loading.value = true
  try {
    const response = await axios.post('/equipment-info/list', {
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })
    if (response.data.code === 1) {
      equipmentList.value = response.data.data.records
      total.value = response.data.data.total
    }
  } catch (error) {
    console.error('获取机器列表失败:', error)
    ElMessage.error('获取机器列表失败')
  } finally {
    loading.value = false
  }
}

// 处理新增
const handleAdd = () => {
  isEdit.value = false
  form.value = {
    id: null,
    equipmentName: '',
    remark: '',
    isDelete: 0
  }
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row) => {
  isEdit.value = true
  form.value = {
    ...row,
    isDelete: typeof row.isDelete === 'number' ? row.isDelete : Number(row.isDelete)
  }
  dialogVisible.value = true
}

// 处理删除
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该机器吗？', '提示', {
      type: 'warning'
    })
    const response = await axios.delete(`/equipment-info/${row.id}`)
    if (response.data.code === 1) {
      ElMessage.success('删除成功')
      fetchEquipmentList()
    } else {
      ElMessage.error(response.data.msg || '删除失败')
    }
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除机器失败:', error)
      ElMessage.error('删除机器失败')
    }
  }
}

// 处理提交
const handleSubmit = async () => {
  try {
    await formRef.value.validate()
    const url = isEdit.value ? '/equipment-info/update' : '/equipment-info/save'
    const response = await axios.post(url, form.value)
    if (response.data.code === 1) {
      ElMessage.success(isEdit.value ? '修改成功' : '添加成功')
      dialogVisible.value = false
      fetchEquipmentList()
    } else {
      ElMessage.error(response.data.msg || '操作失败')
    }
  } catch (error) {
    console.error('提交失败:', error)
    ElMessage.error('表单验证失败')
  }
}

// 处理分页
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchEquipmentList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchEquipmentList()
}

onMounted(() => {
  fetchEquipmentList()
})
</script>

<style scoped>
.equipment-manager {
  padding: 20px;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

:deep(.el-pagination) {
  justify-content: center;
}
</style> 